﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using UnityEditor;
    using UnityEngine;

    internal static class DebugToolbar
    {
        private static void DoDebugGUI()
        {
            int num = FsmErrorChecker.CountAllErrors();
            string text = "No errors";
            if (num > 0)
            {
                text = num + ((num > 1) ? " errors" : "error");
            }
            if (GUILayout.Button(new GUIContent(text, "Error Check"), FsmEditorStyles.ErrorCount, new GUILayoutOption[0]))
            {
                FsmEditor.OpenErrorWindow();
                GUIUtility.ExitGUI();
            }
            if (Event.current.type == EventType.Repaint)
            {
                Rect lastRect = GUILayoutUtility.GetLastRect();
                lastRect.x += 4f;
                lastRect.y += 2f;
                lastRect.width = lastRect.height = 14f;
                FsmEditorUtility.DrawTexture(lastRect, (num > 0) ? FsmEditorStyles.Errors : FsmEditorStyles.NoErrors, Color.white);
            }
            GUILayout.Space(10f);
            if (GUILayout.Button(new GUIContent("Debug", "Debug Options"), EditorStyles.toolbarDropDown, new GUILayoutOption[0]))
            {
                DoDebugMenu();
            }
        }

        private static void DoDebugMenu()
        {
            GenericMenu menu = new GenericMenu();
            menu.AddItem(new GUIContent("Enable Breakpoints"), FsmEditorSettings.BreakpointsEnabled, new GenericMenu.MenuFunction(DebugToolbar.ToggleEnableBreakpoints));
            menu.AddItem(new GUIContent("Clear Breakpoints"), false, new GenericMenu.MenuFunction(FsmEditor.Builder.ClearBreakpoints));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Show State Labels in Game View"), FsmEditorSettings.ShowStateLabelsInGameView, new GenericMenu.MenuFunction(DebugToolbar.ShowStateLabels));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Step Single Frame"), FsmDebugger.Instance.StepMode == FsmDebugger.FsmStepMode.StepFrame, new GenericMenu.MenuFunction(DebugToolbar.StepFrame));
            menu.AddItem(new GUIContent("Step To Next State Change in this FSM"), FsmDebugger.Instance.StepMode == FsmDebugger.FsmStepMode.StepToStateChange, new GenericMenu.MenuFunction(DebugToolbar.StepToStateChange));
            menu.AddItem(new GUIContent("Step To Next State Change in any FSM"), FsmDebugger.Instance.StepMode == FsmDebugger.FsmStepMode.StepToAnyStateChange, new GenericMenu.MenuFunction(DebugToolbar.StepToAnyStateChange));
            menu.AddSeparator("");
            menu.AddItem(new GUIContent("Open Log Window"), false, new GenericMenu.MenuFunction(FsmEditor.OpenFsmLogWindow));
            menu.AddItem(new GUIContent("Enable Logging"), FsmLog.LoggingEnabled, new GenericMenu.MenuFunction(EditorCommands.ToggleLogging));
            menu.ShowAsContext();
        }

        private static void DoPlaybackGUI()
        {
            Color contentColor = GUI.contentColor;
            GUI.contentColor = !FsmEditorStyles.UsingProSkin() ? Color.black : EditorStyles.label.normal.textColor;
            bool changed = GUI.changed;
            GUI.changed = false;
            bool flag2 = GUILayout.Toggle(EditorApplication.isPlayingOrWillChangePlaymode, FsmEditorStyles.Play, EditorStyles.toolbarButton, new GUILayoutOption[] { GUILayout.MaxWidth(40f) });
            if (GUI.changed)
            {
                EditorApplication.isPlaying = flag2;
            }
            else
            {
                GUI.changed = changed;
            }
            changed = GUI.changed;
            GUI.changed = false;
            bool flag3 = GUILayout.Toggle(EditorApplication.isPaused, FsmEditorStyles.Pause, EditorStyles.toolbarButton, new GUILayoutOption[] { GUILayout.MaxWidth(40f) });
            if (GUI.changed)
            {
                EditorApplication.isPaused = flag3;
            }
            else
            {
                GUI.changed = changed;
            }
            if (GUILayout.Button(FsmEditorStyles.Step, EditorStyles.toolbarButton, new GUILayoutOption[] { GUILayout.MaxWidth(40f) }))
            {
                FsmDebugger.Instance.Step();
                DebugFlow.UpdateTime();
                GUIUtility.ExitGUI();
            }
            GUI.contentColor = contentColor;
        }

        private static void DoScrubberGUI()
        {
            if (GameStateTracker.CurrentState == GameState.Stopped)
            {
                GUILayout.FlexibleSpace();
            }
            else if (DebugFlow.Active)
            {
                GUI.contentColor = Color.yellow;
                GUILayout.Label(FsmEditorUtility.FormatTime(DebugFlow.CurrentDebugTime), EditorStyles.toolbarButton, new GUILayoutOption[0]);
                GUI.contentColor = Color.white;
                GUILayout.Space(10f);
                if (GUILayout.Button(new GUIContent("Prev", "Previous Transition"), EditorStyles.toolbarButton, new GUILayoutOption[0]))
                {
                    DebugFlow.SelectPrevTransition();
                }
                if (GUILayout.Button(new GUIContent("Next", "Next Transition"), EditorStyles.toolbarButton, new GUILayoutOption[0]))
                {
                    DebugFlow.SelectNextTransition();
                }
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Open Log", EditorStyles.toolbarButton, new GUILayoutOption[0]))
                {
                    FsmEditor.OpenFsmLogWindow();
                    GUIUtility.ExitGUI();
                }
            }
            else
            {
                GUILayout.Label(FsmEditorUtility.FormatTime(FsmTime.RealtimeSinceStartup), EditorStyles.toolbarButton, new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Open Log", EditorStyles.toolbarButton, new GUILayoutOption[0]))
                {
                    FsmEditor.OpenFsmLogWindow();
                    GUIUtility.ExitGUI();
                }
            }
        }

        public static void OnGUI()
        {
            GUILayout.BeginHorizontal(EditorStyles.toolbar, new GUILayoutOption[0]);
            DoDebugGUI();
            GUILayout.Space(10f);
            DoPlaybackGUI();
            GUILayout.Space(10f);
            DoScrubberGUI();
            GUILayout.EndHorizontal();
        }

        private static void ShowStateLabels()
        {
            FsmEditorSettings.ShowStateLabelsInGameView = !FsmEditorSettings.ShowStateLabelsInGameView;
            PlayMakerGUI.EnableStateLabels = FsmEditorSettings.ShowStateLabelsInGameView;
            FsmEditorSettings.SaveSettings();
        }

        private static void StepFrame()
        {
            FsmDebugger.Instance.StepMode = FsmDebugger.FsmStepMode.StepFrame;
        }

        private static void StepToAnyStateChange()
        {
            FsmDebugger.Instance.StepMode = FsmDebugger.FsmStepMode.StepToAnyStateChange;
        }

        private static void StepToStateChange()
        {
            FsmDebugger.Instance.StepMode = FsmDebugger.FsmStepMode.StepToStateChange;
        }

        private static void ToggleEnableBreakpoints()
        {
            FsmEditorSettings.BreakpointsEnabled = !FsmEditorSettings.BreakpointsEnabled;
            FsmDebugger.EnableBreakpoints(FsmEditorSettings.BreakpointsEnabled);
            FsmEditorSettings.SaveSettings();
        }
    }
}

